﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;

namespace TyNetCoreBlog.Helpers
{
    /// <summary>
    /// 枚举类型的扩展类，获取特性标注内容
    /// </summary>
    public static class EnumExtensions
    {
        public static string GetDescription(this Enum val)
        {
            var field = val.GetType().GetField(val.ToString());
            var customAttribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));
            if (customAttribute == null) 
            { 
                return val.ToString(); 
            }
            else { 
                return ((DescriptionAttribute)customAttribute).Description; 
            }
        }
    }
}
